﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace WMS_Tools.Common
{
    public static class MD5Encode
    {
        /// <summary>
        /// 获得MD5加密字符串
        /// </summary>
        /// <param name="prestr">加密数据</param>
        /// <param name="_input_charset">格式</param>
        /// <returns></returns>
        public static string GetMD5Encode(string prestr, string _input_charset)
        {
            StringBuilder sb = new StringBuilder(32);
            var md5 = MD5.Create();
            byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 简单获取密码强度
        /// </summary>
        /// <param name="prestr"></param>
        /// <param name="_input_charset"></param>
        /// <returns></returns>
        public static byte GetPwdStrength(this string pwd)
        {
            byte result;
            int iLen = pwd.Length;
            // 根据密码长度评分  
            if (iLen <= 4)
            {
                result = 2;
            }
            else if (iLen >= 5 && iLen <= 7)
            {
                result = 1;
            }
            else
            {
                result = 0;
            }
            return result;
        }
    }
}
